<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">

<link rel="stylesheet" href="/css/main.css?v=7.1.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.2">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="lamda表达式java8中lamda表达式的引入，标志着java开始拥抱主流函数式编程语言，其实lamda表达式并不是什么新鲜事物，在JavaScript中早已存在。作为经典语言引入lamda表达式以及java8中的其他新特性，如流式处理，会一定程度上让代码更简洁，当然还是无法和JavaScript这类语言相比，但这也正是各自所处的角色的不同决定的。 lamda表达式的格式 参数类型声明：可以不">
<meta property="og:type" content="article">
<meta property="og:title" content="java8新特性之lamda表达式">
<meta property="og:url" content="http://www.wuxiantao123.cn/2019/06/18/java8新特性之lamda表达式/index.html">
<meta property="og:site_name" content="费曼的门徒">
<meta property="og:description" content="lamda表达式java8中lamda表达式的引入，标志着java开始拥抱主流函数式编程语言，其实lamda表达式并不是什么新鲜事物，在JavaScript中早已存在。作为经典语言引入lamda表达式以及java8中的其他新特性，如流式处理，会一定程度上让代码更简洁，当然还是无法和JavaScript这类语言相比，但这也正是各自所处的角色的不同决定的。 lamda表达式的格式 参数类型声明：可以不">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-18T13:46:02.648Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="java8新特性之lamda表达式">
<meta name="twitter:description" content="lamda表达式java8中lamda表达式的引入，标志着java开始拥抱主流函数式编程语言，其实lamda表达式并不是什么新鲜事物，在JavaScript中早已存在。作为经典语言引入lamda表达式以及java8中的其他新特性，如流式处理，会一定程度上让代码更简洁，当然还是无法和JavaScript这类语言相比，但这也正是各自所处的角色的不同决定的。 lamda表达式的格式 参数类型声明：可以不">





  
  
  <link rel="canonical" href="http://www.wuxiantao123.cn/2019/06/18/java8新特性之lamda表达式/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>java8新特性之lamda表达式 | 费曼的门徒</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">费曼的门徒</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">Feynman follower</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-读书">

    
    
    
      
    

    

    <a href="/books" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>读书</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-计划待办">

    
    
    
      
    

    

    <a href="/todolist" rel="section"><i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>计划待办</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    
  
  

  

  <a href="https://github.com/peach-boy" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewbox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a>



    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.wuxiantao123.cn/2019/06/18/java8新特性之lamda表达式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Thomas Wu">
      <meta itemprop="description" content="人帅也要多读书，多学习，多运动">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="费曼的门徒">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">java8新特性之lamda表达式

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-06-18 16:47:39 / 修改时间：21:46:02" itemprop="dateCreated datePublished" datetime="2019-06-18T16:47:39+08:00">2019-06-18</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/java基础/" itemprop="url" rel="index"><span itemprop="name">java基础</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="lamda表达式"><a href="#lamda表达式" class="headerlink" title="lamda表达式"></a>lamda表达式</h2><p>java8中lamda表达式的引入，标志着java开始拥抱主流函数式编程语言，其实lamda表达式并不是什么新鲜事物，在JavaScript中早已存在。作为经典语言引入lamda表达式以及java8中的其他新特性，如流式处理，会一定程度上让代码更简洁，当然还是无法和JavaScript这类语言相比，但这也正是各自所处的角色的不同决定的。</p>
<h2 id="lamda表达式的格式"><a href="#lamda表达式的格式" class="headerlink" title="lamda表达式的格式"></a>lamda表达式的格式</h2><ol>
<li>参数类型声明：可以不需要声明参数类型，编译器会识别参数值。</li>
<li>参数圆括号（可选）：在单个参数时可以不使用括号，多个参数时必须使用。</li>
<li>大括号和return关键字（可选）：如果只有一个表达式，则可以省略大括号和return关键字，编译器会自动的返回值；相对的，在使用大括号的情况下，则必须指明返回值。</li>
</ol>
<h2 id="lamda表达式例子"><a href="#lamda表达式例子" class="headerlink" title="lamda表达式例子"></a>lamda表达式例子</h2><p>以Collections.sort为例</p>
<pre><code>/**
 * @author xiantao.wu
 * @create 2018/8/1514:09
 **/
public class TestLamda {
    public static void main(String[] args) {
        //原生方法
        List&lt;Student&gt; students1=getStudentList();
        Collections.sort(students1, new Comparator&lt;Student&gt;() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge().compareTo(o2.getAge());
            }
        });
        students1.forEach(in-&gt; System.out.print(in.getName()+in.getAge()+&quot;|&quot;));
        System.out.println(&quot;原生方法&quot;);


        //第一种情况：传入静态方法
        List&lt;Student&gt; students2=getStudentList();
        Collections.sort(students2, (s1, s2) -&gt; Student.sortByAgeStatic(s1, s2));
        students2.forEach(in-&gt; System.out.print(in.getName()+in.getAge()+&quot;|&quot;));
        System.out.println(&quot;第一种情况：传入静态方法&quot;);


        //第二种情况：传入实例方法
        List&lt;Student&gt; students3=getStudentList();
        Collections.sort(students3, (s1, s2) -&gt; new Student().sortByAgeNonStatic(s1, s2));
        students3.forEach(in-&gt; System.out.print(in.getName()+in.getAge()+&quot;|&quot;));
        System.out.println(&quot;第二种情况：传入实例方法&quot;);


        //第三种请款，不适用大括号
        List&lt;Student&gt; students4=getStudentList();
        Collections.sort(students4, (s1, s2) -&gt; s1.getAge().compareTo(s2.getAge()));
        students4.forEach(in-&gt; System.out.print(in.getName()+in.getAge()+&quot;|&quot;));
        System.out.println(&quot;第三种请款，不适用大括号&quot;);


        //第四种情况，使用大括号
        List&lt;Student&gt; students5=getStudentList();
        Collections.sort(students5, (s1, s2) -&gt; {
            return s1.getAge().compareTo(s2.getAge());
        });
        students5.forEach(in-&gt; System.out.print(in.getName()+in.getAge()+&quot;|&quot;));
        System.out.println(&quot;第四种情况，使用大括号&quot;);

    }

    private static List&lt;Student&gt; getStudentList() {
        List&lt;Student&gt; studentList = new ArrayList();
        for (int i = 0; i &lt; 5; i++) {
            Student stu = new Student();
            stu.setAge(new Random().nextInt(10));
            stu.setName(&quot;tom&quot;);
            studentList.add(stu);
        }
        return studentList;
    }


}


/**
 * @author xiantao.wu
 * @create 2018/8/1515:44
 **/
public class Student {
    private Integer age;
    private String name;


    public static int sortByAgeStatic(Student o1, Student o2) {
        return o1.getAge().compareTo(o2.getAge());
    }


    public int sortByAgeNonStatic(Student o1, Student o2) {
        return o1.getAge().compareTo(o2.getAge());
    }


    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}</code></pre><h3 id="测试结果："><a href="#测试结果：" class="headerlink" title="测试结果："></a>测试结果：</h3><pre><code>tom1|tom2|tom3|tom3|tom9|原生方法
tom0|tom0|tom1|tom2|tom4|第一种情况：传入静态方法
tom2|tom2|tom3|tom4|tom8|第二种情况：传入实例方法
tom1|tom1|tom1|tom4|tom8|第三种请款，不适用大括号
tom0|tom1|tom1|tom3|tom7|第四种情况，使用大括号</code></pre>
      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/18/interceptor-配合Threadlocal全局处理请求基本信息/" rel="next" title="interceptor 配合Threadlocal全局处理请求基本信息">
                <i class="fa fa-chevron-left"></i> interceptor 配合Threadlocal全局处理请求基本信息
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/06/18/策略模式替换switch-case/" rel="prev" title="策略模式替换switch case">
                策略模式替换switch case <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Thomas Wu">
            
              <p class="site-author-name" itemprop="name">Thomas Wu</p>
              <div class="site-description motion-element" itemprop="description">人帅也要多读书，多学习，多运动</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">17</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/peach-boy" title="GitHub &rarr; https://github.com/peach-boy" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="/1414924381@qq.com" title="E-Mail &rarr; 1414924381@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/xrq730/" title="https://www.cnblogs.com/xrq730/" rel="noopener" target="_blank">五月的仓颉</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.w3cschool.cn/architectroad/" title="https://www.w3cschool.cn/architectroad/" rel="noopener" target="_blank">架构师之路</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://crossoverjie.top/" title="https://crossoverjie.top/" rel="noopener" target="_blank">crossoverjie</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#lamda表达式"><span class="nav-number">1.</span> <span class="nav-text">lamda表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lamda表达式的格式"><span class="nav-number">2.</span> <span class="nav-text">lamda表达式的格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lamda表达式例子"><span class="nav-number">3.</span> <span class="nav-text">lamda表达式例子</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#测试结果："><span class="nav-number">3.1.</span> <span class="nav-text">测试结果：</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Thomas Wu</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.2"></script>

  <script src="/js/motion.js?v=7.1.2"></script>



  
  


  <script src="/js/affix.js?v=7.1.2"></script>

  <script src="/js/schemes/pisces.js?v=7.1.2"></script>




  
  <script src="/js/scrollspy.js?v=7.1.2"></script>
<script src="/js/post-details.js?v=7.1.2"></script>



  


  <script src="/js/next-boot.js?v=7.1.2"></script>


  

  

  

  


  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
